package com.jiayang.hhotel.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.NowLiveSearchModel;
import com.jiayang.hhotel.ui.NowLiveSearchDetailsActivity;
import com.jiayang.hhotel.util.StringUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/5/23 0023.
 * 现住查询列表适配器
 */

public class NowLiveSearchAdapter extends SwipeMenuAdapter<NowLiveSearchAdapter.ViewHolder> {

    private List<NowLiveSearchModel.PreceptionListDtoBean> mList;
    private Context t;
    public NowLiveSearchAdapter(Context context, List<NowLiveSearchModel.PreceptionListDtoBean> list,int page){
        if(page == 1){
            this.mList = list;
        }else{
            this.mList.addAll(list);
        }
        this.t=context;
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(t).inflate(R.layout.now_live_search_list_item,null);
    }

    @Override
    public ViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new ViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final NowLiveSearchModel.PreceptionListDtoBean model = mList.get(position);
        holder.roomNum.setText(t.getString(R.string.room_num)+t.getString(R.string.space)+model.getRoom());
        holder.level.setText(t.getString(R.string.grade)+t.getString(R.string.space)+model.getRmtype());
        holder.roomPrice.setText(t.getString(R.string.room_price)+t.getString(R.string.space)+model.getFoliorate()+t.getString(R.string.yuan));
        holder.personCount.setText(t.getString(R.string.person_count)+t.getString(R.string.space)+model.getPersons()+t.getString(R.string.ren));
        String[] combination = model.getGstname().split(";");//格式：sex,name;
        holder.addParent.removeAllViews();
        if(!StringUtil.isEmpty(combination[0])){//判空
            for(int i = 0;i < combination.length; i++){
                View item = LayoutInflater.from(t).inflate(R.layout.now_live_search_list_name_item, holder.addParent, false);
                TextView name= (TextView) item.findViewById(R.id.tv_name);
                TextView gender= (TextView) item.findViewById(R.id.tv_gender);
                gender.setText(t.getString(R.string.gender)+t.getString(R.string.space)+(!StringUtil.isEmpty(combination[i].split(",")[0])?combination[i].split(",")[0]:""));
                name.setText(t.getString(R.string.name)+t.getString(R.string.space)+(!StringUtil.isEmpty(combination[i].split(",")[1])?combination[i].split(",")[1]:""));
                holder.addParent.addView(item);
            }
        }
        holder.teamName.setText(t.getString(R.string.team_name)+t.getString(R.string.space)+model.getGrpname());
        holder.arriveDate.setText(t.getString(R.string.arrive_date)+t.getString(R.string.space)+model.getArrdate()+"—"+model.getDptDate());
        holder.details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent start=new Intent(t, NowLiveSearchDetailsActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable(Constant.INTENT_PROJECT_KEY,model);
                start.putExtras(bundle);
                t.startActivity(start);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        ImageView ivZhong;//钟点房
        ImageView ivVip;//vip
        ImageView ivLing;//领
        ImageView ivChuang;//床租房
        ImageView ivYu;//预约房

        TextView roomNum;//房号
        TextView level;//等级
        TextView roomPrice;//房价
        TextView personCount;//人数
        LinearLayout addParent;//添加姓名性别的容器
        TextView teamName;//团名
        TextView arriveDate;//到离日期
        Button details;//查看详情

        public ViewHolder(View itemView) {
            super(itemView);

            ivZhong= (ImageView) itemView.findViewById(R.id.iv_zhong);
            ivVip= (ImageView) itemView.findViewById(R.id.iv_vip);
            ivLing= (ImageView) itemView.findViewById(R.id.iv_ling);
            ivChuang= (ImageView) itemView.findViewById(R.id.iv_chuang);
            ivYu= (ImageView) itemView.findViewById(R.id.iv_yu);

            roomNum= (TextView) itemView.findViewById(R.id.tv_room_num);
            addParent= (LinearLayout) itemView.findViewById(R.id.ll_add_name);
            level= (TextView) itemView.findViewById(R.id.tv_level);
            roomPrice= (TextView) itemView.findViewById(R.id.tv_room_price);
            personCount= (TextView) itemView.findViewById(R.id.tv_person_count);
            teamName= (TextView) itemView.findViewById(R.id.tv_team_name);
            arriveDate= (TextView) itemView.findViewById(R.id.tv_arrive_date);
            details= (Button) itemView.findViewById(R.id.bt_details);
        }
    }
}
